<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row style="margin-bottom: 10px;">
        <el-button @click="delArray(tableChecked)">批量删除</el-button>
      </el-row>
      <el-card shadow="never">
        <el-table
          :key="tableKey"
          :data="dataList"
          v-loading="listLoading"
          element-loading-text="加载中"
          fit
          highlight-current-row
          style="width:100%"
          border
          @selection-change="handleSelectionChange"
          ref="multipleTable"
        >
          <el-table-column type="selection" width="40"></el-table-column>
          <el-table-column align="center" label="评价内容">
            <template slot-scope="scope">
              <span>{{scope.row.commentText}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="评分">
            <template slot-scope="scope">
              <span>{{scope.row.starRank}}</span>星
            </template>
          </el-table-column>
          <el-table-column align="center" label="作者">
            <template slot-scope="scope">
              <span>{{scope.row.nickName}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建日期">
            <template slot-scope="scope">
              <span>{{scope.row.commentDate | dateformat('YYYY-MM-DD HH:mm:ss')}}</span>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
          <el-table-column
            align="center"
            label="操作"
            width="80"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-link
                type="danger"
                :underline="false"
                @click="removeEvaluation(scope.row.commentId)"
              >删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
          <div class="pages" style="float:right;margin-bottom:20px;">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="Number(requestParameters.pageNo)"
              :total="Number(total)"
              :page-size="Number(requestParameters.pageSize)"
              :page-sizes="[10,20,30,50]"
              layout="sizes, prev, pager, next, jumper"
            ></el-pagination>
          </div>
        </div>
        <!-- end -->
      </el-card>
    </div>
  </div>
</template>
<script>
import { list, remove, batch } from '@/api/xczx/evaluation'
var _this = null
export default {
  name: 'Evaluation',
  components: {},
  data() {
    return {
      multipleSelection: [],
      tableChecked: [],
      ids: [],
      commentIds: [],
      tableKey: 0,
      dataList: [],
      total: null,
      listLoading: true,
      alertText: '',
      requestParameters: {
        pageNo: 1,
        pageSize: 10
      },
      requestBody: {}
    }
  },
  computed: {
    status() {
      return status
    }
  },
  methods: {
    initialDate() {
      this.getList()
    },
    handleSelectionChange(val) {
      this.tableChecked = val
    },
    // 获取列表数据
    async getList(params) {
      this.listLoading = true
      const { data: res } = await list(this.requestParameters)
      this.dataList = res.items
      // 获取总条数
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
      this.listLoading = false
    },
    // 重置
    resetForm() {
      this.$refs['requestParameters'].resetFields()
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.requestParameters.pageSize = val
      if (this.requestParameters.pageNo === 1) {
        this.getList(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.requestParameters.pageNo = val
      this.getList()
    },
    // 评论-批量删除
    delArray(rows) {
      var _this = this
      const length = this.tableChecked.length
      if (length == 0) {
        this.$message({
          message: '选择要删除的评价内容！',
          center: true
        })
        return
      }
      this.$confirm('确定要删除选中的内容吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(() => {
          this.change(rows)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async change(rows) {
      rows.forEach(element => {
        this.ids.push(element.commentId)
      })
      let data = this.ids.toString()

      await batch(data).then(() => {
        this.getList()
      })
    },
    // 评价-单条删除
    removeEvaluation(commentId) {
      this.$confirm('确定要删除评价内容 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ commentId: commentId })
            .then(response => {
              this.$message.success('成功删除了评价内容' + '!')
              // this.dataList.splice(user, 1)
              this.getList(this.requestParameters)
            })
            .catch(response => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    }
  },
  // 挂载结束
  mounted: function() {},
  // 创建完毕状态
  created() {
    var _this = this
    this.initialDate()
    // 键盘enter操作
    document.onkeydown = function(e) {
      var key = window.event.keyCode
      if (key === 13) {
        _this.handleFilter(this.requestParameters)
      }
    }
  },
  // 组件更新
  updated: function() {}
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
</style>

<style>
.el-table th {
  background-color: #ffffff;
}
.el-table th.is-leaf {
  border-bottom: 2px solid #e8e8e8;
}
.disabled td {
  background-color: #f9f9f9;
  color: #c1c1c1;
}
.disabled .el-button--primary,
.disabled .el-button--danger {
  background-color: #dbdada;
  border: 1px solid #dbdada;
  color: #999;
  cursor: not-allowed;
}
</style>
